<?php

if ($do === 'editEvt') {
   if ($_REQUEST[evtAction]) {
      foreach ($_REQUEST[evtAction] as $evt) {
         if ($_REQUEST[editEvt] === 'delEvt') {
            mysql_query(" DELETE FROM `schedule` WHERE `id`='".$evt."' ");
         }
      }
   }
} else if ($do === 'addEvt') {
   if ($_REQUEST[evtMulti] == 2) {
      if ($_REQUEST[evtWeekday] === 'weekdays') $evtWeekday = array(1,2,3,4,5);
      else if ($_REQUEST[evtWeekday] === 'daily') $evtWeekday = array(1,2,3,4,5,6,7);
      else if ($_REQUEST[evtWeekday] === 'weekdends') $evtWeekday = array(6,7);
      else if ($_REQUEST[evtWeekday] === 'mwf') $evtWeekday = array(1,3,5);
      else if ($_REQUEST[evtWeekday] === 'th') $evtWeekday = array(2,4);
      else $evtWeekday[0] = $_REQUEST[evtWeekday];
      
      for ($i=0; $i <= count($evtWeekday); $i++) {       
         if ($_REQUEST[evtRepLength] == 1 ) {
            $evtRepStarttime = strtotime('00:00:00');
            $evtRepEndtime = strtotime('23:59:59');
         } else {
            $evtRepStarttime = strtotime($_REQUEST[evtRepStarttime]);
            $evtRepEndtime = strtotime($_REQUEST[evtRepEndtime]);
         }
         $evtTimeOffset = strtotime('00:'.$_REQUEST[evtTimeOffset]) - mktime(0,0,0);
         $evtRepFreq = $_REQUEST[evtRepFreq];
         
         $evtTime = $evtRepStarttime;
         echo ($evtTime +  $evtTimeOffset.'/'.$evtRepEndtime.'<br />');
         while ( ($evtTime + $evtTimeOffset) <= $evtRepEndtime) {
            if ( ($evtRepFreq == 1) || ((date('H', $evtTime) % 2) && ($evtRepFreq == 3)) || ( !(date('H', $evtTime) % 2) && ($evtRepFreq == 2) ) ) {
               mysql_query ("
                  INSERT INTO 
                  `schedule`
                  SET 
                  `weekday`='".$evtWeekday[$i]."',
                  `eventtype`='".$_REQUEST[evtType]."',
                  `starttime`='".date('H:i:s', ($evtTime + $evtTimeOffset))."'
               ");
            }
            $evtTime += 3600;
         }
      }
   } else {
      mysql_query (" 
         INSERT INTO `schedule` 
         SET 
         `weekday`='".$_REQUEST[evtWeekday]."',
         `eventtype`='".$_REQUEST[evtType]."',
         `starttime`='".$_REQUEST[evtStarttime]."',
         `endtime`='".$_REQUEST[evtEndtime]."'
          ");
   }
} else if ($do === 'schedClearDay') {
   mysql_query ("DELETE FROM `schedule` WHERE `weekday`='".$_REQUEST[schedWeekday]."' ");
}

$weekday = array (
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
7 => 'Sunday'
);

?>
<form name="trafSched" action="?mode=trafSched" method="post">
<table><tr><td>
   <table>
   <tr>
   <td>Add Event: </td>
   <td><select name="evtType"><?php
      $evtQuery = mysql_query("SELECT * FROM `event` ORDER BY `name` ASC ");
      while ($evt = mysql_fetch_array($evtQuery)) {
         printf('<option value="%s">%s</option>', $evt[id], trunc($evt[name], 16));
      }
      ?></select></td>
   </tr>
   
   <tr>
   <td>Weekday: </td>
   <td><select name="evtWeekday"><?php
   for ($i=1; $i<8; $i++) {
      printf('<option value="%s">%s</option>', $i, $weekday[$i]);
   }
   ?><option value="daily">everyday</option>
   <option value="weekdays">weekdays</option>
   <option value="weekends">weekends</option>
   <option value="mwf">Mon/Wed/Fri</option>
   <option value="th">Tue/Thur</option>
   </select>
   </td>
   </tr>
   
   <tr>
   <td>Add Multiple? </td>
   <td><select name="evtMulti" id="trafSchedEvtMulti" onChange="schedEvtMulti()">
   <option value="1">single event</option>
   <option value="2">multiple events</option>
   </select></td>
   </tr>
   
   </table>
</td>
<td>

<span id="schedEvtMultiDisplay"><table>
<tr>
<td>Start Time </td>
<td><input type="text" name="evtStarttime" size="5" /> (24-hour format, required)</td>
</tr>
<tr>
<td>End Time </td>
<td><input type="text" name="evtEndtime" size="5" /> (24-hour, optional)</td>
</tr><tr><td colspan="2"><input type="hidden" name="do" value="addEvt" />
<input type="submit" value="Add Event" /></td></tr>
</table></span>
</td></tr>
</table>

</form>

<?php 
$evtQuery = mysql_query (" SELECT * FROM `event` ");
while ($evt = mysql_fetch_array($evtQuery)) {
   $evtName[($evt[id])] = $evt[name];
}
?>

<span id="trafSched">
Schedule:<br />
<form action="?mode=trafSched" method="post">
<table>
<tr>
<?php
$j=0;
for ($i=1; $i<8; $i++) {
   echo('<td><table><tr><td>'.$weekday[$i].' (<a onClick="if (confirm(\'WARNING: Clearing the schedule for this day will IRREVERSIBLY delete all of its events! If you are sure you wish to do this, select OK, otherwise click Cancel. \')) parent.frames[\'main\'].location = \'?mode=trafSched&schedWeekday='.$i.'&do=schedClearDay\' ">Clear</a>)</td></tr>');
   
   $schedQuery = mysql_query("SELECT * FROM `schedule` WHERE `weekday`='".$i."' ORDER BY `starttime` ASC ");
   while ($sched = mysql_fetch_array($schedQuery)) {
      printf('<tr><td><input type="checkbox" name="evtAction[%s]" value="%s" /> %s - %s</td></tr>', $j++, $sched[id],  $evtName[($sched[eventtype])], $sched[starttime] );
   }
   echo('</td></tr></table></td>');
}
?>
</tr>
</table>
<select name="editEvt"><option value="">With Selected:</option><option value="delEvt">Delete</option></select>
<input type="hidden" name="do" value="editEvt" />
<input type="submit" value="Save Changes" />
</form>
</span>